<?php
/**
 * @Name: Role.php
 * @Author: yashuai<1762910894@qq.com>
 */

namespace app\backstage\controller\rbac;

use HugCode\PhpUnits\Frame\Code\ConstCode;
use think\Request;
use think\facade\Db;
use app\common\requests\StatusRequest;
use app\common\services\rbac\RoleServices;
use app\backstage\controller\BaseController;
use app\common\requests\rbac\RoleNodeRequest;
use app\common\requests\rbac\RoleSaveRequest;

class Role extends BaseController
{

    public function __construct(RoleServices $service)
    {
        $this->service = $service;
    }

    /**
     * @Desc 列表
     * @param Request $request
     * @return \think\response\Json
     * @author yashuai<1762910894@qq.com>
     */
    public function index(Request $request)
    {
        $params = $request->all();
        $result = $this->service->getList($params);
        return _success($result);
    }

    /**
     * @Desc 添加
     * @param RoleSaveRequest $request
     * @return \think\response\Json
     * @author yashuai<1762910894@qq.com>
     */
    public function create(RoleSaveRequest $request)
    {
        $params = $request->getValidationData();
        $result = $this->service->createRow($params);
        return _success($result);
    }

    /**
     * @Desc 编辑
     * @param RoleSaveRequest $request
     * @return \think\response\Json
     * @author yashuai<1762910894@qq.com>
     */
    public function update(RoleSaveRequest $request)
    {
        $validator = [
            'RoleID' => 'require|integer'
        ];
        $params    = $request->getValidationData($validator);
        $result    = $this->service->updateRow($params);
        return _success($result);
    }

    /**
     * @Desc 修改状态
     * @param StatusRequest $request
     * @return \think\response\Json
     * @author yashuai<1762910894@qq.com>
     */
    public function status(StatusRequest $request)
    {
        $params = $request->getValidationData();
        $this->service->updateStatus($params['ID'], $params['Status'], 'RoleStatus');
        return _success();
    }

    /**
     * @Desc 权限的菜单节点
     * @param RoleNodeRequest $request
     * @return mixed
     * @author yashuai<1762910894@qq.com>
     */
    public function node(RoleNodeRequest $request)
    {
        $params = $request->getValidationData();
        return Db::transaction(function () use ($params) {
            $this->service->node($params);
            return _success();
        });
    }

    /**
     * @Desc 获取下拉框数据
     * @return \think\response\Json
     * @author yashuai<1762910894@qq.com>
     */
    public function select()
    {
        $result = $this->service->select(['RoleStatus' => ConstCode::BASIC_STATUS_NORMAL], 'RoleName', 'RoleID');
        return _success($result);
    }

}
